package class34;

import java.util.HashMap;

/**
 * @author zhangchaoliang
 * create 2022
 */
public class CutArray {

    public static boolean cutArray(int[] arr) {
        if (arr == null || arr.length < 7) {
            return false;
        }
        HashMap<Integer, Integer> map = new HashMap<>();
        int sum = arr[0];
        for (int i = 1; i < arr.length; i++) {
            map.put(sum, i);
            sum += arr[i];
        }
        int n = arr.length;
        int a = arr[0];
        for (int i = 1; i < n - 5; i++) {
            if (map.containsKey(a) && map.get(a) == i) {
                int cut2 = 2 * a + arr[i];
                if (map.containsKey(cut2)) {
                    int cut3 = 3 * a + arr[i] + arr[map.get(cut2)];
                    if (map.containsKey(cut3)) {
                        int index = map.get(cut3);
                        int lastSum = cut3 + arr[index] + a;
                        if (lastSum == sum) {
                            return true;
                        }
                    }
                }
            }
            a += arr[i];
        }
        return false;
    }
}
